const xlsx = require('node-xlsx').default;
const fs = require('fs')
const path = require('path')

const API_PATH = require('./utils/API_PATH');
const { post } = require('./utils/request')

const { genData, returnText } = require('./utils/util')

// 常量
const ZXBM = '1301232005'
// 01045
// 01049
// 01056
const DXBH = '01049' || process.argv[2] // 01099
const sxmc = '先锋模范记录'
const arrSx = [
  '党员姓名',
  '所属党支部',
  '所属机构',
  '性别',
  '登记时间',
  '先锋模范事迹',
]

async function main() {

  // 获取平台定义数据
  async function getCfg(P, myMap) {
    await post(API_PATH.PLATFORM_CONFIG, P).then(res => {
      res.results.forEach(item => {
        myMap.set(item.num, item.name)
        myMap.set(item.num1, item.name)
      })
    }).catch(err => {
      console.log(P.type, err)
    })
  }
  // 时间统计周期
  const timeCycle = new Map()
  await getCfg({ type: "sjtjzq" }, timeCycle)
  // console.log('timeCycle', timeCycle)

  // 对象定义属性类别
  const dxdy_sxlb = new Map()
  await getCfg({ type: "dxdy_sxlb" }, dxdy_sxlb)
  // console.log('dxdy_sxlb', dxdy_sxlb)

  // 组件样式
  const componentStyle = new Map()
  await getCfg({ type: "componentStyle" }, componentStyle)
  // console.log('componentStyle', componentStyle)

  // 对象指标统计类型
  const dxzbtjlx = new Map()
  await getCfg({ type: "dxzbtjlx" }, dxzbtjlx)
  // console.log('dxzbtjlx', dxzbtjlx)

  // 对象指标空间类型
  const dxzbkjlx = new Map()
  await getCfg({ type: "dxzbkjlx" }, dxzbkjlx)
  // console.log('dxzbkjlx', dxzbkjlx)

  // 属性密级
  const sxmj = new Map()
  await getCfg({ type: "sxmj" }, sxmj)
  // console.log('sxmj', sxmj)

  // 登录信息
  const sxdy_dlxx = new Map()
  await getCfg({ type: "sxdy_dlxx" }, sxdy_dlxx)
  // console.log('sxdy_dlxx', sxdy_dlxx)

  // componentType
  const cmpTypeJe = new Map()
  await getCfg({ type: "componentType", num2: "InputNumber" }, cmpTypeJe)
  // console.log('cmpTypeJe', cmpTypeJe)

  // componentType
  const zbdw = new Map()
  await getCfg({ type: "zbdy" }, zbdw)
  // console.log('zbdw', zbdw)

  // init prototype
  const initPrototype = new Map()
  await post(API_PATH.INIT_PROTOTYPE, { isShow: 1, page: 0, size: 0, syObjectNumber: " " })
    .then(res => {
      res.results.forEach(item => {
        initPrototype.set(item.fieldIdentification, item.fieldName)
      })
    })
  // console.log('initPrototype', initPrototype)

  // all prototype
  const AllPrototype = []
  await post(API_PATH.ALL_PROTOTYPE, { attributeLevel: 1, fieldName: "", isLastStage: 1, organizationNumber: ZXBM, page: 1, size: 990, syObjectNumber: DXBH, })
    .then(res => {
      res.results.forEach(item => {
        item.attributeType = JSON.parse(item.attributeType)
        AllPrototype.push(item)
      })
    })
  // console.log('AllPrototype', AllPrototype.slice(AllPrototype.length - 5))

  // 对象实例查询
  const ArrDxslls = []
  await post(API_PATH.MANGE_OBJECT_EXTEND, { extendClass: "dxslls", organizationNumber: ZXBM, page: 1, size: 10, syObjectNumber: DXBH, })
    .then(res => {
      res.results.forEach(item => {
        item.note = JSON.parse(item.note)
        ArrDxslls.push(item)
      })
    })

  // console.log(ArrDxslls)

  // // 报表
  // const dxdybb = []
  // await post(API_PATH.MANGE_OBJECT_EXTEND, { extendClass: "dxdybb", organizationNumber: ZXBM, page: 1, size: 10, syObjectNumber: DXBH, })
  //   .then(res => {
  //     res.results.forEach(item => {
  //       item.note = JSON.parse(item.note)
  //       dxdybb.push(item)
  //     })
  //   })

  // console.log(dxdybb)

  // 指标
  const ArrDxdyzb = []
  await post(API_PATH.MANGE_OBJECT_EXTEND, { extendClass: "dxdyzb", organizationNumber: ZXBM, page: 1, size: 10, syObjectNumber: DXBH, })
    .then(res => {
      res.results.forEach(item => {
        item.note = JSON.parse(item.note)
        ArrDxdyzb.push(item)
      })
    })
  // console.log('ArrDxdyzb', ArrDxdyzb)

  // 对象定义中的事项
  const ArrDxdySx = []
  await post(API_PATH.MATTER_SUBJECT, { organizationNumber: ZXBM, page: 1, size: 10, syObjectNumber: DXBH, })
    .then(res => {
      res.results.forEach(item => {
        ArrDxdySx.push(item)
      })
    })
  // console.log('ArrDxdySx', ArrDxdySx)

  // 对象定义这个对象
  const SYObject = await post(API_PATH.MANGE_OBJECT, { organizationNumber: ZXBM, page: 1, size: 10, syObjectNumber: DXBH, })
    .then(res => {
      return res.results[0]
    })
  // console.log('SYObject', SYObject)



  // Parse a file
  const workSheetsFromFile = [
    { name: '系统测试用例', data: [[]] }
  ]
  // 第一个表的列宽
  const sheetOptions = { '!cols': new Array(40).fill({ wch: 25 }) };

  // 第一个表

  const data = workSheetsFromFile[0].data
  let curCol = 0 // 第几列
  let curRow = 0 // 第几行


  curCol = 1
  curRow = 1

  // 属性
  for (let i = 0; i < arrSx.length; i++) {
    curCol = 2

    let item = AllPrototype.find(item => item.fieldName === arrSx[i])

    let arr = []
    if (['input', 'InputNumber'].includes(item.attributeType.sxys)) {
      arr = returnText(item, sxmc)
    } else {
      arr = genData(sxmc, item)
    }

    data.push(...arr)

  }


  ; (function test() {
    // 头部信息
    // for (let i = 1; i < 20; i++) {
    //   console.log(data[i])
    // }
    // console.log(AllPrototype[0])
    // for (let i = 12; i < 12 + AllPrototype.length; i++) {
    //   console.log(data[i])
    // }
  })();

  let buffer = xlsx.build(workSheetsFromFile.slice(0, 2), { sheetOptions });
  // console.log(workSheetsFromFile)

  fs.writeFileSync(`./build/${SYObject.syObjectName}测试用例.xlsx`, buffer,)

}

main()
